package com.nifostasky.fragment;
/**
 * 链接界面
 */

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.nifostasky.comic.ChapterActivity;
import com.nifostasky.comic.ComicPicture;
import com.nifostasky.comic.MainActivity;
import com.nifostasky.comic.R;
import com.nifostasky.utils.JsoupContent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainFragment1 extends Fragment {
    private static final File file = new File("data/data/com.nifostasky.comic/cache/info.txt");
    //声明控件
    private Button mbtn_fragment1 = null;
    private EditText met_fragment1 = null;
    View view  = null;
    /**
     * 获取view界面
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //获取视图
        view = inflater.inflate(R.layout.activity_main_fragment1,null);
        //获取控件
        mbtn_fragment1 = view.findViewById(R.id.btn_fragment1);
        met_fragment1 = view.findViewById(R.id.et_fragment1);
        //将内容填入文本框
        this.extractFile();


        //设置按键点击事件
        setListeners();


        return view;
    }


    //活动
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    //设置按键点击事件
    private void setListeners(){
        Listener listener = new Listener();
        mbtn_fragment1.setOnClickListener(listener);
    }


    //点击事件实现
    private class Listener implements View.OnClickListener{
        String url = null;
        boolean flag = false;
        boolean isValid = false;
        @Override
        public void onClick(View v) {
            Intent intent = null;

            // 按键事件定义
            switch (v.getId()){
                case R.id.btn_fragment1:
                    System.out.println("按键被点击");
                    //获取文本框中的内容
                    url = met_fragment1.getText().toString().trim();
                    System.out.println(url);


                    //线程判断url地址是否正确
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            isValid = JsoupContent.isValid(Listener.this.url);
                            System.out.println(" 判断" + isValid);
                            flag = true;
                        }
                    };
                    new Thread(runnable).start();





                    while (true){
                        if(flag) {
                            if (isValid) {
                                //将内容保存入文件
                                MainFragment1.this.saveFile(url);
                                //定义意图并,确定转发位置
                                intent = new Intent();
                                intent.setClass(view.getContext(), ChapterActivity.class);
                                //设置需要传递的参数
                                intent.putExtra("url", url);
                                //执行意图进行跳转
                                System.out.println("按键被点击1");
                                startActivity(intent);
                            } else {
                                Toast.makeText(view.getContext(), "您输入的网址不是有效网址", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        }
                    }
                    break;
            }


        }
    }

    //保存上次输入的链接,(文件)
    private void saveFile(String  url){
        FileOutputStream fos=null;
        try {
            fos = new FileOutputStream(file);
            fos.write(url.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos !=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //将文件提取出来并填入输入框
    private void extractFile(){
        if(file.exists()&&file.length()>0) {
            BufferedReader br = null;
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                br = new BufferedReader(new InputStreamReader(fis));
                String info = br.readLine();
                met_fragment1.setText(info);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
